package com.bdqn.t382.filter;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getRequestURI().contains("/login") || request.getRequestURI().contains("/doLogin")) {
            return true;
        }
        Object user = request.getSession().getAttribute("user");
        // 未登录，重定向到登录页面
        if (user == null) {
            response.sendRedirect("/ch04/login");
            return false;
        } else {
            return true;
        }
    }
}
